package game.managers
{
	import flash.utils.Dictionary;
	/**
	 * 文字配置文件处理
	 * 
	 */
	public class LanguageManager
	{
		private static var _words:Dictionary = new Dictionary();
		private static var _familyNames:Dictionary = new Dictionary();
		
		/** 
		 * 初始化解析资源数据 
		 * @param data
		 */		
		public static function setup(data:String):void
		{
			_words = new Dictionary();
			
			var t:String = data;
			data = data.split("\\n").join("\n");
			var list:Array = data.split("\r\n");
			
			for(var i:int = 0; i < list.length; i++){
				var s:String = list[i];
				if(s == "")continue;
				if(s.indexOf("//") == 0)continue;
				var n:int = s.indexOf(":");
				if(n != -1){
					var name:String  = s.substring(0,n);
					var value:String = s.substr(n + 1);
					_words[name]     = value;
				}
			}
		}
		
		/**
		 * 预定义方法
		 * 提供以后开发接口 
		 * @param data
		 */		
		public static function setupToo(data:String):void
		{
			if(!data) return ;
			_familyNames = new Dictionary();
			
			var t:String = data;
			data = data.split("\\n").join("\n");
			var list:Array = data.split("\r\n");
			
			for(var i:int = 0; i < list.length; i++){
				var s:String = list[i];
				if(s == "")continue;
				if(s.indexOf("//") == 0)continue;
				var n:int = s.indexOf(":");
				if(n != -1){
					var name:String = s.substring(0,n);
					var value:String = s.substr(n + 1);
					_familyNames[name] = value;
				}
			}
		}
		
		/**
		 * 预定义方法
		 * 提供以后开发接口 
		 * @param data
		 */	
		public static function getNames(id:String,...args):String
		{
			var s:String = _familyNames[id];
			if(s != null){
				if(args.length > 0){
					for(var i:int = 0; i < args.length; i++){
						s = s.split("{" + (i + 1) + "}").join(args[i]);
					}
				}
				return s;
			}
			return "";
		}
		
		/**
		 * @param id 通过id获取
		 * @param args
		 * @return value
		 */		
		public static function getWord(id:String,...args):String
		{
			var s:String = _words[id];
			if(s != null){
				if(args.length > 0){
					for(var i:int = 0; i < args.length; i++){
						s = s.split("{" + (i + 1) + "}").join(args[i]);
					}
				}
				return s;
			}
			return "";
		}
		
	}
}